package cn.rdq.iknesssystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.rdq.api.dto.DramaOption;
import cn.rdq.api.po.Drama;
import cn.rdq.api.vo.DramaVo;
import cn.rdq.dao.DramaMapper;
import cn.rdq.iknesssystem.service.DramaService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DramaServiceImpl extends ServiceImpl<DramaMapper, Drama> implements DramaService {


    @Override
    public List<Drama> dramaList(DramaVo dramaVo) {
        QueryWrapper<Drama> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(dramaVo.getTitle())) {
            wrapper.like("title", dramaVo.getTitle());
        }
        if (!StringUtils.isEmpty(dramaVo.getCategory())) {
            wrapper.eq("category", dramaVo.getCategory());
        }
        if (!StringUtils.isEmpty(dramaVo.getArea())) {
            wrapper.eq("area", dramaVo.getArea());
        }
        if (!StringUtils.isEmpty(dramaVo.getStatus())) {
            wrapper.eq("status", dramaVo.getStatus());
        }
        wrapper.orderByDesc("create_time");
        PageHelper.startPage(dramaVo.getCurrentPage(), dramaVo.getPageSize());
        return list(wrapper);
    }

    @Override
    public List<DramaOption> dramaOptions() {
        QueryWrapper<Drama> wrapper = new QueryWrapper<>();
        wrapper.select("id", "title");
        List<Drama> dramas = list(wrapper);
        return BeanUtil.copyToList(dramas, DramaOption.class);
    }

    @Override
    public boolean saveDrama(Drama drama) {
        drama.setCreateTime(new Date());
        drama.setUpdateTime(new Date());
        drama.setSubTitle(drama.getOthTitle());
        return save(drama);
    }

    @Override
    public Drama queryOne(DramaVo dramaVo) {
        QueryWrapper<Drama> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(dramaVo.getTitle())) {
            wrapper.eq("title", dramaVo.getTitle());
        }
        return getOne(wrapper);
    }
}
